package com.ruoyi.msg.controller;

import java.util.List;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import cn.dev33.satoken.annotation.SaIgnore;
import com.ruoyi.msg.domain.MsgSwiper;
import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.msg.domain.vo.MsgSwiperVo;
import com.ruoyi.msg.domain.bo.MsgSwiperBo;
import com.ruoyi.msg.service.IMsgSwiperService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 轮播图信息
 *
 * @author strongme
 * @date 2023-05-23
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/msg/swiper")
public class MsgSwiperController extends BaseController {

    private final IMsgSwiperService iMsgSwiperService;

    @GetMapping("listActiveSwiperList")
    @SaIgnore
    public R<List<MsgSwiperVo>> listActiveSwiperList() {
        MsgSwiperBo bo = new MsgSwiperBo();
        bo.setStatus("0");
        return R.ok(iMsgSwiperService.queryList(bo));
    }

    /**
     * 查询轮播图信息列表
     */
    @SaCheckPermission("msg:swiper:list")
    @GetMapping("/list")
    public TableDataInfo<MsgSwiperVo> list(MsgSwiperBo bo, PageQuery pageQuery) {
        return iMsgSwiperService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出轮播图信息列表
     */
    @SaCheckPermission("msg:swiper:export")
    @Log(title = "轮播图信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(MsgSwiperBo bo, HttpServletResponse response) {
        List<MsgSwiperVo> list = iMsgSwiperService.queryList(bo);
        ExcelUtil.exportExcel(list, "轮播图信息", MsgSwiperVo.class, response);
    }

    /**
     * 获取轮播图信息详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("msg:swiper:query")
    @GetMapping("/{id}")
    public R<MsgSwiperVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(iMsgSwiperService.queryById(id));
    }

    /**
     * 新增轮播图信息
     */
    @SaCheckPermission("msg:swiper:add")
    @Log(title = "轮播图信息", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody MsgSwiperBo bo) {
        return toAjax(iMsgSwiperService.insertByBo(bo));
    }

    /**
     * 修改轮播图信息
     */
    @SaCheckPermission("msg:swiper:edit")
    @Log(title = "轮播图信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody MsgSwiperBo bo) {
        return toAjax(iMsgSwiperService.updateByBo(bo));
    }

    /**
     * 删除轮播图信息
     *
     * @param ids 主键串
     */
    @SaCheckPermission("msg:swiper:remove")
    @Log(title = "轮播图信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iMsgSwiperService.deleteWithValidByIds(Arrays.asList(ids), true));
    }
}
